{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "771dbca8-2daf-4d7a-987d-b30c6ed58e89",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a94bb5cb-bc7e-4bf6-a4e7-b08eb5599ce4",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.arange(16).reshape(4,2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ce0a5ab7-a138-4a81-a90f-4175df4bd6ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.numel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4b4b0512-c974-44e8-9acf-db95a8c79e44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 2, 2])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0bb94902-a696-4bca-b6ae-9a2863aa4d1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = torch.zeros((2,3,4))\n",
    "c = torch.ones((2,3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0b8a5459-4cc3-4743-90ea-e0c972e29423",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 3, 4])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "df5383db-90ba-456c-b212-661864c463e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]],\n",
       "\n",
       "        [[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7a8f9bbf-620e-4878-bb8a-f327e856d686",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 3],\n",
       "        [0, 0, 0],\n",
       "        [9, 9, 9]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor([[1,2,3],[0,0,0],[9,9,9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b44f90c9-9ee1-46a5-9f88-20a67ab23fac",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = torch.tensor([1.0, 2, 4, 8])\n",
    "y = torch.tensor([2, 2, 2, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bf39de03-ce66-40a6-9ea8-7344245ef9bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 3.,  4.,  6., 10.]),\n",
       " tensor([-1.,  0.,  2.,  6.]),\n",
       " tensor([ 2.,  4.,  8., 16.]),\n",
       " tensor([0.5000, 1.0000, 2.0000, 4.0000]),\n",
       " tensor([ 1.,  4., 16., 64.]))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x + y, x - y, x * y, x / y, x ** y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "191237bb-c5d6-4925-b718-94a4110b198c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.exp(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d81c826f-5e2a-4f36-9e77-5b882445347f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X= torch.arange(12,dtype=torch.float32).reshape(3,4)\n",
    "Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1f1e8c72-0296-494d-a22d-2ddbc8a47df7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [ 2.,  1.,  4.,  3.],\n",
       "         [ 1.,  2.,  3.,  4.],\n",
       "         [ 4.,  3.,  2.,  1.]]),\n",
       " tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],\n",
       "         [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]]))"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cat((X,Y), dim=0), torch.cat((X, Y),dim= 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cb2cc013-f95b-4e2c-ad48-7432e397f109",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[False,  True, False,  True],\n",
       "        [False, False, False, False],\n",
       "        [False, False, False, False]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X == Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "85c0fdb5-eb37-43e7-aa7b-c2dee07b1454",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(66.)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cbc8cccf-9550-49b8-8d2b-146a656897b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.arange(3).reshape((3, 1))\n",
    "b = torch.arange(2).reshape((1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "0057bccf-7133-4db7-9351-c8f2b57fc02d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0],\n",
       "         [1],\n",
       "         [2]]),\n",
       " tensor([[0, 1]]))"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a, b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "294d2f30-4c9b-425e-be19-b54749a77b53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1],\n",
       "        [1, 2],\n",
       "        [2, 3]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "893df8b7-dbee-4960-953f-97d8dc288978",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139836991584992\n"
     ]
    }
   ],
   "source": [
    "before = id(Y)\n",
    "print(before)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f93104b1-6127-4d2f-a0e2-3046ab29cde7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139836991583632\n"
     ]
    }
   ],
   "source": [
    "Y = X+Y\n",
    "print(id(Y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0324506b-05ad-47fc-a578-e44c3c77a08f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(Y) == before"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "184c5bd5-ecee-4072-85ca-6391b51eaa5b",
   "metadata": {},
   "source": [
    "1、原地操作，避免重新分配内存,采用切片方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4fd11d0b-98c3-45f3-b00c-770ae7072754",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139836991584752\n",
      "139836991584752\n"
     ]
    }
   ],
   "source": [
    "Z= torch.zeros_like(Y)\n",
    "print(id(Z))\n",
    "Z[:] = X+Y\n",
    "print(id(Z))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d358a6b3-5398-43bd-9505-c156550a1e9a",
   "metadata": {},
   "source": [
    "1、原地操作，避免重新分配内存,采用+=方式,这里很重要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1fc95f67-d0fd-4dc9-bd5e-178ae63065d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139836991583632\n",
      "139836991583632\n"
     ]
    }
   ],
   "source": [
    "print(id(Y))\n",
    "Y += X\n",
    "print(id(Y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e777840-12c2-4125-9df1-fc809c65dbd4",
   "metadata": {},
   "source": [
    "将深度学习框架定义的张量转换为NumPy张量（ndarray）很容易，反之也同样容易。 torch张量和numpy数组将共享它们的底层内存，就地操作更改一个张量也会同时更改另一个张量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "644340a9-3bd9-4b7e-a1d6-c276773ae305",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(numpy.ndarray, torch.Tensor)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = X.numpy()\n",
    "B = torch.tensor(A)\n",
    "type(A), type(B)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "d5530328-da98-4658-bc2f-e9006b4328de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139836994123216\n",
      "139836991584512\n"
     ]
    }
   ],
   "source": [
    "print(id(A))\n",
    "print(id(B))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4bbe0fb-1c43-4689-8d79-0c954f072363",
   "metadata": {},
   "source": [
    "要将大小为1的张量转换为Python标量，我们可以调用item函数或Python的内置函数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "d57fb808-a96b-44db-b00c-38144aab5fa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([3.5000]), 3.5, 3.5, 3)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.tensor([3.5])\n",
    "a, a.item(), float(a), int(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e08abd1a-2f3c-47fb-94d4-1963fee45a94",
   "metadata": {},
   "source": [
    "运行本节中的代码。将本节中的条件语句X == Y更改为X < Y或X > Y，然后看看你可以得到什么样的张量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "71057747-1ec4-4ca8-b8ec-85dcdd4b7695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[False, False, False, False],\n",
       "        [False, False, False, False],\n",
       "        [False, False, False, False]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X>Y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35289004-4072-4d56-be62-81b807d540d7",
   "metadata": {},
   "source": [
    "用其他形状（例如三维张量）替换广播机制中按元素操作的两个张量。结果是否与预期相同？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f616fd8-fa31-40d7-b1c7-5abe909e643e",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.arange(3).reshape((3, 1,1))\n",
    "b = torch.arange(2).reshape((1, 2,1))\n",
    "a, b"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
